define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/index' + location.search,
                    add_url: 'goods/add',
                    edit_url: 'goods/edit',
                    del_url: 'goods/dels',
                    multi_url: 'goods/multi',
                    import_url: 'goods/import',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                exportOptions: { ignoreColumn: [10, 'operate', 'avatar']},
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'admin.username', title: __('Admin.username'), operate: 'LIKE'},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'goodsonetype.name', title: __('Goodsonetype.name'), operate: 'LIKE'},
                        {field: 'goodstwotype.name', title: __('Goodstwotype.name'), operate: 'LIKE'},
                        {field: 'goodsthreetype.name', title: __('Goodsthreetype.name'), operate: 'LIKE'},
                        {field: 'vip_price', title: __('vip价格'), operate: false},

                        {field: 'province', title: __('省'), operate: false},
                        {field: 'city', title: __('市'), operate: false},

                        {field: 'code', title: __('Code'), operate: 'LIKE'},
                        {field: 'chandi', title: __('Chandi'), operate: 'LIKE'},
                        {field: 'brand.name', title: __('Brand.name'), operate: 'LIKE'},
                        //{field: 'parameter', title: __('Parameter'), operate: 'LIKE'},
                        //{field: 'introduce', title: __('Introduce'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        //{field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'cost_price', title: __('Cost_price')},
                        {field: 'price', title: __('Price')},
                        //{field: 'old_price', title: __('Old_price'), operate:'BETWEEN'},
                        {field: 'sale_num', title: __('Sale_num')},
                        {field: 'integral', title: __('Integral')},
                        {field: 'expressfee.name', title: __('Expressfee.name'), operate: 'LIKE'},
                        //{field: 'expressfee_id', title: __('Expressfee_id')},
                        //{field: 'zhongliang', title: __('Zhongliang'), operate:'BETWEEN'},
                        //{field: 'is_baoyou', title: __('Is_baoyou'), searchList: {"1":__('Is_baoyou 1'),"2":__('Is_baoyou 2')}, formatter: Table.api.formatter.normal},
                        //{field: 'is_del', title: __('Is_del'), searchList: {"1":__('Is_del 1'),"0":__('Is_del 0')}, formatter: Table.api.formatter.normal},
                        //{field: 'author_id', title: __('Author_id')},
                        //{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'spec1', title: __('Spec1'), operate: 'LIKE'},
                        //{field: 'spec2', title: __('Spec2'), operate: 'LIKE'},
                        {field: 'add_switch', title: __('Add_switch'), searchList: {"1":__('Add_switch 1'),"2":__('Add_switch 2')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'is_speciality', title: __('是否为特产'), searchList: {"0":__('否'),"1":__('是')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'is_handpick', title: __('是否为精选'), searchList: {"0":__('否'),"1":__('是')}, table: table, formatter: Table.api.formatter.toggle},
                        //{field: 'uptime', title: __('Uptime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'supplier.suppname', title: __('Supplier.suppname'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'id', title: __('一级属性设置'), formatter: Controller.api.formatter.id,operate:false},
                        {field: 'id', title: __('二级属性设置'), formatter: Controller.api.formatter2.id,operate:false},
                        {field: 'id', title: __('规格设置'), formatter: Controller.api.formatter3.id,operate:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                showToggle: false,//显示隐藏列可以快速切换字段列的显示和隐藏，如果不需要此功能，可以设置showColumns: false，如果想要表格中的字段列默认隐藏可以设置字段属性visible: false即可默认隐藏
                // showColumns: false,
                search:false
            });

            // 为表格绑定事件
            Table.api.bindevent(table);


            $("#exp").bind("click",function(){
                var options = table.bootstrapTable('getOptions');
                var search = options.queryParams({});
                var str=location.search+"&filter="+search.filter+"&op="+search.op;
                window.location.href = 'goods/export'+str  ;
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                id: function (value, row, index) {
                    //这里手动构造URL
                    var ids = row['id'];
                    url = "goods_spec_one?ids=" + ids;
                    //方式一,直接返回class带有addtabsit的链接,这可以方便自定义显示内容
                    return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("属性一", value) + '">' + '属性一' + '</a>';
                }
            },
            formatter2: {
                id: function (value, row, index) {
                    //这里手动构造URL
                    var ids = row['id'];
                    url = "goods_spec_two?ids=" + ids;
                    //方式一,直接返回class带有addtabsit的链接,这可以方便自定义显示内容
                    return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("属性二", value) + '">' + '属性二' + '</a>';
                }
            },
            formatter3: {
                id: function (value, row, index) {
                    //这里手动构造URL
                    var ids = row['id'];
                    url = "goods_stock?ids=" + ids;
                    //方式一,直接返回class带有addtabsit的链接,这可以方便自定义显示内容
                    return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("规格设置", value) + '">' + '规格设置' + '</a>';
                }
            }
        }
    };
    return Controller;
});